import { loadEnv, type UserConfigFnObject } from "vite";
import userConfig from "./config";
import alias from "./alias";
import setupPlugins from "./plugins";
import { parseEnv } from "./env";
import buildProxy from "./proxy";

const buildViteConfig: UserConfigFnObject = ({ mode }) => {
    if (userConfig) return userConfig;
    const root = process.cwd();
    const env = parseEnv(loadEnv(mode, root));
    const proxy = buildProxy(env);
    return {
        plugins: setupPlugins(mode === 'production'),
        resolve: {
            alias
        },
        server: {
            port: env.VITE_PORT,
            proxy
        }
    }
}

export default buildViteConfig;